/*
 * @Author: your name
 * @Date: 2021-01-06 11:41:48
 * @LastEditTime: 2021-01-23 23:03:06
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \express-seed\service\imgService.js
 */
const path = require('path')
const fs = require("fs"); //node读取文件模块
const { getSellerDao, addCodeDao, delSellerDao, updateSellerDao, getSellerbyStartDao } = require('../dao')

module.exports = {
    //查询销售
    getSellerService: async (page_id) => {
        return await getSellerDao(page_id)
    },
    //获取启用状态销售列表
    getSellerbyStartService: async (page_id) => {
        return await getSellerbyStartDao(page_id)
    },
    //添加销售信息
    addCodeService: async (codeInfo) => {
        // 更具数据库返回结果写入图片
         return await addCodeDao(codeInfo)
    },
    //删除销售
    delSellerService: async (img_id, img_name) => {
        const { status } = await delSellerDao(img_id)
        if (status) {
            const imgUrl = path.resolve(__dirname, `../public/img/${img_name}`)
            const delResult = await new Promise((resolve, reject) => {
                fs.unlink(imgUrl, (error) => {
                    if (error) {
                        console.log('删除本地文件失败', error);
                        return resolve(false)
                    }
                    console.log('删除文件成功');
                    return resolve(true)
                })
            })
            return delResult
            // return true
        }
    },
    //更新销售人信息
    updateSellerService: async (sellerInfo) => {
        const { status } = await updateSellerDao(sellerInfo)
        return status
    }
}
